Ext.define('PJ.view.unitregistrationmanagement.ChooseNoUserList', {
    extend: 'AM.default.DefaultList',
    alias: 'widget.ChooseNoUserList',
    
    initComponent: function(){
        this.columns = [{
            header: '学员帐号',
            dataIndex: 'bu.login_ID',
            width: 75
        }, {
            header: '姓名',
            dataIndex: 'c.user_Name',
            width: 75
        }, {
            header: '性别',
            dataIndex: 'c.sex_code',
            width: 50
        }, {
            header: '出生年月',
            dataIndex: 'birthdayStr',
            width: 90,
            renderer: function(value, meta, record){
                meta.tdAttr = 'data-qtip="' + value + '"';
                return value;
            }
        },{
            header: '民族',
            dataIndex: 'c.folk_Code',
            width: 50,
            renderer: function(value, meta, record){
                meta.tdAttr = 'data-qtip="' + value + '"';
                return value;
            }
        },{
            header: '手机号',
            dataIndex: 'c.mobile_Telephone',
//            hidden:true,
            width: 100
        }, {
            header: '单位',
            dataIndex: 'd.depname',
            flex: 1,
            minWidth: 120,
            renderer: function(value, meta, record){
                meta.tdAttr = 'data-qtip="' + value + '"';
                return value;
            }
        },{
            header: '职级',
            dataIndex: 'rank',
            flex:.55,
            renderer: function(value, meta, record){
                meta.tdAttr = 'data-qtip="' + value + '"';
                return value;
            }
        }, {
            header: '单位及职务',
            dataIndex: 'c.duty_Remark',
            flex: 1,
            minWidth: 120,
            renderer: function(value, meta, record){
                meta.tdAttr = 'data-qtip="' + value + '"';
                return value;
            }
        } /*{
            header: '联系电话',
            dataIndex: 'c.mobile_Telephone',
            flex: 1,
            minWidth: 120,
            renderer: function(value, meta, record){
                meta.tdAttr = 'data-qtip="' + value + '"';
                return value;
            }
        }, {
            header: '备注',
            dataIndex: 'c.remark',
            flex: 1,
            minWidth: 120,
            renderer: function(value, meta, record){
                meta.tdAttr = 'data-qtip="' + value + '"';
                return value;
            }
        }*/];

//        classId: me.up('window').classId,//培训班Id
        this.store = Ext.create('PJ.' + prototype + '.unitregistrationmanagement.ChooseNoUserList', {
            isLoad: false
        });
        
        var me = this;
//    	this.addListener("itemclick", function(view, record, htmlelement, index, eventObject, object){
//    		if(eventObject){
//    			eventObject.stopEvent();
//    		}
//    		
////    		var gridListStore = this.up("container").down("ChooseUserTree").getStore();
////    		var records = me.getSelectionModel().getSelection();
////			var newParams = {
////					searchDepId: records[0].data.id,
////					searchClassID: me.up('window').classId
////			};
////			var params = this.store.proxy.extraParams;
////			Ext.apply(params, newParams);
////			store.load();
//    	});
        //实现排序序号的编辑
        this.plugins = [Ext.create('Ext.grid.plugin.CellEditing', {
            clicksToEdit: 1,
            listeners: {
                beforeedit: function(editor, e){
                    var textfield = e.column.getEditor();
                    if (gj_onlineLearning_constant.loadType == "jsonp") {
                        var storeToken = Ext.create('AM.default.jsonp.DefaultAjaxStore', {
                            loadType: gj_onlineLearning_constant.loadType,
                            url: gj_onlineLearning_constant.tokenUrl
                        });
                        storeToken.load(function(rec, operation, success){
                            var token = operation.response;
                            textfield.token = token;
                        });
                    }
                    else 
                        if (gj_onlineLearning_constant.loadType == "ajax") {
                            Ext.Ajax.request({
                                url: tokenUrl,
                                success: function(response){
                                    var data = response.responseText;
                                    var token = Ext.decode(data, true);
                                    textfield.token = token;
                                }
                            });
                        }
                }
            }
        })];
        this.loadType = gj_onlineLearning_constant.loadType;
        this.callParent(arguments);
    },
    
    //报名
    onAddClick: function(button){
        var me = this;
        Ext.create('PJ.view.registrationmanagement.ChooseUserEdit', {
            title: "报名信息",
            flag: 1,
            grid: me,
            userId:null,
            openState: me.openState,//培训班状态
            classId: me.classId,//培训班Id
			clickSource:me.clickSource,//点击来源
        	sponsorId:me.sponsorId,
            signUpState: me.signUpState//报名状态
        }).show();
    },
    //修改
    onUpdateClick: function(button){
        var me = this;
        var records = this.getSelectionModel().getSelection();
        if (records.length > 1) {
            Ext.Msg.show({
                title: '提示',
                msg: '只允许操作一条数据！',
                icon: Ext.Msg.WARNING,
                buttons: Ext.Msg.OK
            });
            return;
        }
        var win = Ext.create('PJ.view.registrationmanagement.ChooseUserEdit', {
            title: "修改报名信息",
            flag: 2,
            grid: me,
            userId: records[0].get("userId"),
            openState: me.up('window').openState,//培训班状态
            classId: me.up('window').classId,//培训班Id
            signUpState: me.up('window').signUpState//报名状态
        }).show();
        win.down('form').getForm().load({
            url: me.store.preUpdateUrl,
            params: {
                searchUserId: records[0].get("userId")
            },
            success: function(baseForm, action){
                //                baseForm.findField("tpassword").setValue(action.result.data.password);
            }
        });
    },
    //撤销报名
    onUnSignUpClick: function(button){
        var me = this;
        var records = this.getSelectionModel().getSelection();
        Ext.Msg.confirm({
            title: '提示',
            msg: '您确定要为这些学员撤销报名吗？',
            buttons: Ext.Msg.OKCANCEL,
            fn: function(id){
                if (id == 'ok') {
                    var classUserIds = new Array();
                    var userIds = new Array();
                    Ext.Array.each(records, function(name, index){
                        classUserIds.push(name.get("classUserId"));
                        userIds.push(name.get("userId"));
                    });
                    if (gj_onlineLearning_constant.loadType == "jsonp") {
                        var store1 = Ext.create('AM.default.jsonp.DefaultAjaxStore', {
                            loadType: gj_onlineLearning_constant.loadType,
                            url: me.getStore().unSignUpUrl,
                            extraParams: {
                                searchClassUserIds: classUserIds,
                                searchUserIds: userIds,
                                openState: me.up('window').openState
                            }
                        });
                        store1.load(function(rec, operation, success){
                            var msg = success == true ? '操作成功！' : operation.error;
                            Orchid.alert('消息', msg, function(){
                                var store = me.getStore();
                                store.load();
//                                me.quotaOfPeople();//预授人数统计
                            });
                        });
                    }
                    else 
                        if (gj_onlineLearning_constant.loadType == "ajax") {
                            Ext.Ajax.request({
                                url: me.getStore().unSignUpUrl,
                                params: {
                                    searchClassUserIds: classUserIds,
                                    searchUserIds: userIds,
                                    openState: me.up('window').openState
                                },
                                success: function(response){
                                    var data = response.responseText;
                                    var json = Ext.decode(data, true);
                                    var msg = json.success == true ? '操作成功！' : json.msg;
                                    Orchid.alert('消息', msg, function(){
                                        var store = me.getStore();
                                        store.load();
//                                        me.quotaOfPeople();//预授人数统计
                                    });
                                }
                            });
                        }
                }
                else {
                    return;
                }
            }
        });
    },
    //点击报名
    onSelectClick: function(){
        var me = this;
        var store=me.getStore();
        var records = me.getSelectionModel().getSelection();
        if (records.length == 0) {
        	Ext.Msg.show({
                title: '提示',
                msg: '请至少选择一个学员才可以进行报名！',
                icon: Ext.Msg.WARNING,
                buttons: Ext.Msg.OK
            });
    		return;
        } else {
            var classId = me.classId;
//            var signUpState=me.signUpState;
            var ids = new Array();
            for (i in records) {
            	if(isNaN(i)){
            		break;
            	}
                ids.push(records[i].data.userId);
            }
			var recodes=me.up('window').down("ChooseUserTree").getSelectionModel().getSelection();
//            var deptId=recodes.length==0?$(document.getElementById("selectScope")).find("option:selected")[0].value:recodes[0].data.parentId;
            var deptId=me.sponsorId;
            var classIds = new Array(['2c9095d15f4de4f6015f4de7e2e90001'],['2c9095d15f4de4f6015f4defdcf7000a'],['2c9095d15f4de4f6015f4df2249c000d'],['2c9095d15f4de4f6015f4df3aac00010'],['2c9095d15f4de4f6015f4df608fe0013'],['2c9095d15f4de4f6015f4df764da0016']);
            if(classId == '2c9095d15f4de4f6015f4de7e2e90001' || classId == '2c9095d15f4de4f6015f4defdcf7000a' || classId == '2c9095d15f4de4f6015f4df2249c000d' || classId == '2c9095d15f4de4f6015f4df3aac00010' || classId == '2c9095d15f4de4f6015f4df608fe0013' || classId == '2c9095d15f4de4f6015f4df764da0016'){
            	Ext.Ajax.request({
					url:store.isFiveClassUrl,
					params: {
						searchClassIds: classIds,
						searchUserIds:ids
					},
					success:function(response){
						var data=response.responseText;
						var json=Ext.decode(data,true);
						if(json.success){
							if(json.msg == ''){
								win = Ext.create('PJ.view.unitregistrationmanagement.CloseUserListFrame',{
				        			title: "确认报名人员",
				        			win: me,
				        			deptId: deptId,
				        			userIds:ids,
				        			auditingState:me.auditingState,
				        			clickSource:me.clickSource,
				        			classId:classId,
//				        			token: token
				        		}).show();
							}else{
								Ext.Msg.show({
					                title: '提示',
					                msg: json.msg,
					                icon: Ext.Msg.WARNING,
					                buttons: Ext.Msg.OK
					            });
//								Orchid.alert('提示',json.msg,function(){
//									//store.load();
//								});
							}
						}else{
							Orchid.alert('提示',json.msg,function(){
								store.load();
							});
						}
					}
				});
            }else{
            	win = Ext.create('PJ.view.unitregistrationmanagement.CloseUserListFrame',{
        			title: "确认报名人员",
        			win: me,
        			deptId: deptId,
        			userIds:ids,
        			auditingState:me.auditingState,
        			clickSource:me.clickSource,
        			classUserNum:me.classUserNum,
        			classId:classId,
//        			token: token
        		}).show();
            }
//            Ext.Ajax.request({
//                url: tokenUrl,
//                success: function(response){
//                    var data = response.responseText;
//                    var token = Ext.decode(data, true);
                    
//                }
//            });
        }
    },
    //批量导入
    onImportClick:function(){
    	var me = this;
  		Ext.create("PJ.view.unitregistrationmanagement.Upload", {
  			title: "批量导入报名人员",
  			height: 140,
  			width: 350,
  			win: me,
  			auditingState: me.auditingState,//报名状态
			clickSource:me.clickSource,//点击来源
  			classId: me.classId
  		}).show();
    },
    listeners: {
        //点击列表数据时显示按钮事件
        selectionchange: function(selModel, selected){
        	this.down('#' + this.oldId + "Add").setDisabled(selected.length === 0);//报名
        },
        viewready: function(){
            this.addDocked([{
                dock: 'top',
                xtype: "searchform",
                columnCount: 1,
                fieldDefaults: {
    				labelAlign: 'left',
    				labelWidth: 70
    			},
                items: [{
                    xtype: "textfield",
                    name: "searchLoginID",
    				labelAlign: 'right',
        			columnWidth: .5,
                    emptyText: '请输入学员帐号',
                    fieldLabel: '学员帐号'
                },{
                    xtype: "textfield",
                    name: "searchUserName",
        			columnWidth: .5,
    				labelAlign: 'right',
                    emptyText: '请输入姓名，若多个姓名请以“，”隔开',
                    fieldLabel: '姓名'
                },{
                    xtype: "textfield",
                    name: "searchDutyRemark",
    				labelAlign: 'right',
        			columnWidth: .5,
                    emptyText: '请输入单位及职务',
                    fieldLabel: '单位及职务'
                },{
                	xtype:"combo",
                    fieldLabel: "职级",
                    name: "searchRank",
                    emptyText: "请选择",
    				labelAlign: 'right',
                    columnWidth:.5,
                	store: Ext.create("Ext.data.ArrayStore", {
                		singleton: true,
                		fields: [{
                			name: 'value',
                			mapping: "dataCode",
                			type: 'string'
                		}, {
                			name: 'text',
                			mapping: "dataName",
                			type: 'string'
                		}],
                		proxy: {
                			type: gj_onlineLearning_constant.loadType,
                			url: this.store.rankTypeUrl + '?typeCode=1228',
                			reader: Ext.create('Ext.data.reader.Json', {
                				type: 'json',
                				root: 'data',
                				successProperty: 'success'
                			})
                		},
                		autoLoad: true
                	}),
        			valueField: 'value',
        			displayField: 'text',
        			editable: false,
        			defaultActive: false
                }],
    			onResetClick: function(button){
                    var form = button.up('form');
                    var gridPanel = form.grid;
        			var gridList = gridPanel.getStore();
        			var recodes=gridPanel.up('window').down("ChooseUserTree").getSelectionModel().getSelection();
//        			var deptId=recodes.length==0?$(document.getElementById("selectScope")).find("option:selected")[0].value:recodes[0].data.id;
        			var deptId=gridPanel.sponsorId;
        			var deptName=gridPanel.sponsorName;
        			gridList.recordId = deptId;
        			gridList.recordName = deptName;

                    var newParams = {
                    		searchDepId: deptId,
        					searchClassID: this.up('window').classId
                    };
                    gridPanel.getStore().proxy.extraParams = {};
                    var params = gridPanel.getStore().proxy.extraParams;
                    Ext.apply(params, newParams);
                    form.getForm().reset();
                },
                grid: this
            },{
                xtype: 'toolbar',
                dock: 'top',
                //displayInfo: true,
                items: [{
                    text: '报名',
                    iconCls: 'signUpIco',
                    itemId: this.oldId + "Add",
                    disabled: true,
                    scope: this,
                    handler: this.onSelectClick
                }, /*"-",{
                    text: '新增',
                    iconCls: 'signUpAddIco',
                    itemId: this.oldId + "Add2",
                    disabled: false,
                    scope: this,
                    handler: this.onAddClick
                }, */"-",{
                    text: '导入',
                    iconCls: 'signUpImpIco',
                    itemId: this.oldId + "Import",
                    disabled: false,
                    scope: this,
                    handler: this.onImportClick
                } /*, "-", {
                 text: '修改',
                 iconCls: 'updateIco',
                 itemId: this.oldId + "Update",
                 disabled: true,
                 scope: this,
                 handler: this.onUpdateClick
                 }, "-", {
                 text: '撤销报名',
                 iconCls: 'publishCancelIco',
                 itemId: this.oldId + "UnSignUp",
                 disabled: true,
                 scope: this,
                 handler: this.onUnSignUpClick
                 }, "-", {
                 xtype: 'label',
                 itemId: 'unitQuotaOfPeople',
                 html: ''
                 }*/
                , "->", "->", this.countSelectPanel]
            }]);
            // 触发权限过滤事件
            this.fireEvent("permission", this);
        }
    }
});
